package com.example.yls.window;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class FloatingButtonService extends Service {
    public static boolean isStarted=false;
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private Button button;

    @Override
    public void onCreate() {
        super.onCreate();
        isStarted=true;
        windowManager= (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams=new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        layoutParams.type=WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else{
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format= PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 500;
        layoutParams.height = 100;
        layoutParams.x = 300;
        layoutParams.y = 300;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showFloatingWindow();
        return super.onStartCommand(intent, flags, startId);
    }


    private void showFloatingWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)){
                button=new Button(getApplicationContext());
                button.setText("window");
                button.setBackgroundColor(Color.BLUE);
                windowManager.addView(button, layoutParams);
                button.setOnTouchListener(new FloatOnTouchListen());

            }
        }else{
            button=new Button(getApplicationContext());
            button.setText("window");
            button.setBackgroundColor(Color.BLUE);
            windowManager.addView(button, layoutParams);
            button.setOnTouchListener(new FloatOnTouchListen());
        }
    }

   private class FloatOnTouchListen implements View.OnTouchListener {
       private int x;
       private int y;
       @Override
       public boolean onTouch(View view, MotionEvent motionEvent) {
           switch (motionEvent.getAction()) {
               case MotionEvent.ACTION_DOWN:
                   x = (int) motionEvent.getRawX();
                   y = (int) motionEvent.getRawX();

                   break;
               case MotionEvent.ACTION_MOVE:
                   int nowX = (int) motionEvent.getRawX();
                   int nowY = (int) motionEvent.getRawY();
                   int movedX = nowX - x;
                   int movedY = nowY - y;
                   x = nowX;
                   y = nowY;
                   layoutParams.x = layoutParams.x + movedX;
                   layoutParams.y = layoutParams.y + movedY;
                   windowManager.updateViewLayout(view, layoutParams);
                   break;
               default:
                   break;

           }

           return false;
       }
   }
}
